/**
 * @Date 2025-09-24 18:51:40
 * @AIDE AIDE+
 */
package com.goldsprite.gameframeworks.tiledmap;

import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.maps.MapObjects;
import com.badlogic.gdx.maps.objects.RectangleMapObject;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.Array;

public class MapManager {
    public TiledMap tiledMap;
    private Array<Rectangle> collisionRects;

	public void loadMap(String mapPath) {
        // 加载TMX地图
        tiledMap = new TmxMapLoader().load(mapPath);
        collisionRects = new Array<>();

        // 读取碰撞层对象
        readCollisionObjects();
    }

    private void readCollisionObjects() {
        // 获取名为"碰撞层"的对象层
        MapObjects objects = tiledMap.getLayers().get("CollisionObjects").getObjects();

        // 遍历所有对象
        for (MapObject object : objects) {
            // 检查是否为矩形对象
            if (object instanceof RectangleMapObject) {
                RectangleMapObject rectObject = (RectangleMapObject) object;
                Rectangle rectangle = rectObject.getRectangle();

                // 将矩形添加到碰撞列表
                collisionRects.add(rectangle);

                // 打印调试信息
                System.out.println("碰撞框: " + object.getName() +
								   " - 位置: (" + rectangle.x + ", " + rectangle.y +
								   ") 尺寸: " + rectangle.width + "x" + rectangle.height);
            }
        }

        System.out.println("共加载 " + collisionRects.size + " 个碰撞框");
    }

    // 获取地图用于渲染
    public TiledMap getTiledMap() {
        return tiledMap;
    }

    // 获取所有碰撞框（用于调试绘制）
    public Array<Rectangle> getCollisionRects() {
        return collisionRects;
    }

    public void dispose() {
        if (tiledMap != null) {
            tiledMap.dispose();
        }
    }
}
